package com.quarkus.cloud.service;

import com.quarkus.cloud.domain.entity.User;
import io.quarkus.hibernate.reactive.panache.PanacheQuery;
import io.quarkus.hibernate.reactive.panache.common.WithSession;
import io.quarkus.hibernate.reactive.panache.common.WithTransaction;
import io.quarkus.panache.common.Page;
import io.quarkus.panache.common.Sort;
import io.smallrye.mutiny.Uni;
import jakarta.inject.Singleton;

/**
 * @author zhuhb-a
 */
@Singleton
public class UserService {

    @WithTransaction
    public Uni<User> findById(Long id) {
        return User.findById(id);
    }

    @WithSession
    public Page page(String name, Integer pageNo, Integer pageSize) {
        PanacheQuery<User> query = User.find("name", Sort.ascending("id"), name);
        query.page(Page.of(pageNo, pageSize));
        return query.page();
    }

    @WithTransaction
    public Uni<Void> insert(User user) {
        return User.persist(user);
    }

    @WithTransaction
    public Uni<Integer> update(User user) {
        return User.update("update t_user set name = ?1 where id = ?2", user.getName(), user.getId());
    }

    @WithTransaction
    public Uni<Boolean> delete(Long id) {
        return User.deleteById(id);
    }

}


